home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JViewport.java < prev    next >
Text File  |  1998-06-30  |  26KB  |  798 lines

  1. /*
  2.  * @(#)JViewport.java    1.37 98/03/20
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import com.sun.java.swing.event.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.accessibility.*;
  26.  
  27. import java.awt.Component;
  28. import java.awt.Container;
  29. import java.awt.LayoutManager;
  30. import java.awt.Dimension;
  31. import java.awt.Rectangle;
  32. import java.awt.Point;
  33. import java.awt.Insets;
  34. import java.awt.Graphics;
  35. import java.awt.Image;
  36. import java.awt.event.ComponentListener;
  37. import java.awt.event.ComponentAdapter;
  38. import java.awt.event.ComponentEvent;
  39.  
  40. import java.io.Serializable;
  41.  
  42.  
  43. /**
  44.  * The "viewport" or "porthole" through which you see the underlying 
  45.  * information. When you scroll, what moves is the viewport. Its like
  46.  * peering through a camera's viewfinder. Moving the viewfinder upwards 
  47.  * brings new things into view at the top of the picture and loses 
  48.  * things that were at the bottom.
  49.  * <p>
  50.  * Warning: serialized objects of this class will not be compatible with
  51.  * future swing releases.  The current serialization support is appropriate
  52.  * for short term storage or RMI between Swing1.0 applications.  It will
  53.  * not be possible to load serialized Swing1.0 objects with future releases
  54.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  55.  * baseline for the serialized form of Swing objects.
  56.  *
  57.  * @version 1.37 03/20/98
  58.  * @author Hans Muller
  59.  * @author Philip Milne
  60.  * @see JScrollPane
  61.  */
  62. public class JViewport extends JComponent implements Accessible
  63. {
  64.     /** True when the viewport dimensions have been determined. */
  65.     protected boolean isViewSizeSet = false;
  66.  
  67.     /**
  68.      * The last viewPosition that we've painted, so we know how
  69.      * much of the backing store image is valid.
  70.      */
  71.     protected Point lastPaintPosition = null;
  72.  
  73.     /** 
  74.      * True when this viewport is maintaining an offscreen image of its
  75.      * contents, so that some scrolling can take place using fast "bit-blit"
  76.      * operations instead of by accessing the view object to construct the 
  77.      * display.
  78.      */
  79.     protected boolean backingStore = false;
  80.     /** The view image used for a backing store. */
  81.     transient protected Image backingStoreImage = null;
  82.  
  83.     /**
  84.      * The scrollUnderway flag is used for components like JList.
  85.      * When the downarrow key is pressed on a JList and the selected
  86.      * cell is the last in the list, the scrollpane autoscrolls.
  87.      * Here, the old selected cell needs repainting and so we need
  88.      * a flag to make the viewport do the optimised painting
  89.      * only when there is an explicit call to setViewPosition(Point).
  90.      * When setBounds() is called through other routes,
  91.      * the flag is off and the view repaints normally.
  92.      * Another approach would be to remove this from the Viewport
  93.      * class and have the JList manage this case by using
  94.      * setBackingStoreEnabled().
  95.      */
  96.     protected boolean scrollUnderway = false;
  97.  
  98.     /*
  99.      * Listener that's notified each time the view changes size.
  100.      */
  101.     private ComponentListener viewListener = null;
  102.  
  103.     /* Only one ChangeEvent is needed per JViewport instance since the
  104.      * event's only (read-only) state is the source property.  The source
  105.      * of events generated here is always "this".
  106.      */
  107.     private transient ChangeEvent changeEvent = null;
  108.  
  109.     /** Create a JViewPort */
  110.     public JViewport() {
  111.         super();
  112.         setLayout(createLayoutManager());
  113.     }
  114.  
  115.     /**
  116.      * Sets the Viewport's one lightweight child, which can be null.
  117.      * (Since there is only one child which occupies the entire viewport,
  118.      * the constraints and index arguments are ignored.)
  119.      *
  120.      * @param child       the Component ______________
  121.      * @param constraints the Object ______________
  122.      * @param index       the int ______________
  123.      * @see #setView
  124.      */
  125.     protected void addImpl(Component child, Object constraints, int index) {
  126.       setView(child);
  127.     }
  128.  
  129.  
  130.     /**
  131.      * Removes the Viewport's one lightweight child.
  132.      *
  133.      * @see #setView
  134.      */
  135.     public void remove(Component child) {
  136.         child.removeComponentListener(viewListener);
  137.         super.remove(child);
  138.     }
  139.  
  140.  
  141.     /**
  142.      * Overridden to scroll the view so that Rectangle within the
  143.      * view becomes visible.
  144.      *
  145.      * @param contentRect the Rectangle to display
  146.      */
  147.     public void scrollRectToVisible(Rectangle contentRect) {
  148.         Component view = getView();
  149.  
  150.         if (view == null) {
  151.             return;
  152.         } else {
  153.             int     dx = 0, dy = 0;
  154.             Rectangle bounds = getBounds();
  155.  
  156.             dx = positionAdjustment(bounds.width, contentRect.width, contentRect.x);
  157.             dy = positionAdjustment(bounds.height, contentRect.height, contentRect.y);
  158.  
  159.             if (dx != 0 || dy != 0) {
  160.                 Point viewPosition = getViewPosition();
  161.                 setViewPosition(new Point(viewPosition.x - dx, viewPosition.y - dy));
  162.                 scrollUnderway = false;
  163.             }
  164.         }
  165.     }
  166.  
  167.      /*  This method is used by the scrollToRect method to determine the
  168.       *  proper direction and amount to move by. The integer variables are named
  169.       *  width, but this method is applicable to height also. The code assumes that
  170.       *  parentWidth/childWidth are positive and childAt can be negative.
  171.       */
  172.     private int positionAdjustment(int parentWidth, int childWidth, int childAt)    {
  173.  
  174.         //   +-----+
  175.         //   | --- |     No Change
  176.         //   +-----+
  177.         if (childAt >= 0 && childWidth + childAt <= parentWidth)    {
  178.             return 0;
  179.         }
  180.  
  181.         //   +-----+
  182.         //  ---------   No Change
  183.         //   +-----+
  184.         if (childAt <= 0 && childWidth + childAt >= parentWidth) {
  185.             return 0;
  186.         }
  187.  
  188.         //   +-----+          +-----+
  189.         //   |   ----    ->   | ----|
  190.         //   +-----+          +-----+
  191.         if (childAt > 0 && childWidth <= parentWidth)    {
  192.             return -childAt + parentWidth - childWidth;
  193.         }
  194.  
  195.         //   +-----+             +-----+
  196.         //   |  --------  ->     |--------
  197.         //   +-----+             +-----+
  198.         if (childAt >= 0 && childWidth >= parentWidth)   {
  199.             return -childAt;
  200.         }
  201.  
  202.         //   +-----+          +-----+
  203.         // ----    |     ->   |---- |
  204.         //   +-----+          +-----+
  205.         if (childAt <= 0 && childWidth <= parentWidth)   {
  206.             return -childAt;
  207.         }
  208.  
  209.         //   +-----+             +-----+
  210.         //-------- |      ->   --------|
  211.         //   +-----+             +-----+
  212.         if (childAt < 0 && childWidth >= parentWidth)    {
  213.             return -childAt + parentWidth - childWidth;
  214.         }
  215.  
  216.         return 0;
  217.     }
  218.  
  219.  
  220.     /**
  221.      * The viewport "scrolls" it's child (called the "view") by the
  222.      * normal parent/child clipping (typically the view is moved in
  223.      * the opposite direction of the scroll).  A non-null border,
  224.      * or non-zero insets, isn't supported, to prevent the geometry
  225.      * of this component from becoming complex enough to inhibit
  226.      * subclassing.  To create a JViewport with a border, add it to a
  227.      * JPanel that has a border.
  228.      *
  229.      * @param border the Border to set
  230.      */
  231.     public final void setBorder(Border border) {
  232.         if (border != null) {
  233.             throw new IllegalArgumentException("JViewport.setBorder() not supported");
  234.         }
  235.     }
  236.  
  237.  
  238.     /**
  239.      * Returns the insets (border) dimensions as (0,0,0,0), since borders
  240.      * are not supported on a JViewPort.
  241.      *
  242.      * @return new Insets(0, 0, 0, 0)
  243.      * @see #setBorder
  244.      */
  245.     public final Insets getInsets() {
  246.         return new Insets(0, 0, 0, 0);
  247.     }
  248.  
  249.  
  250.     private Graphics getBackingStoreGraphics(Graphics g) {
  251.         Graphics bsg = backingStoreImage.getGraphics();
  252.         bsg.setColor(g.getColor());
  253.         bsg.setFont(g.getFont());
  254.         bsg.setClip(g.getClipBounds());
  255.         return bsg;
  256.     }
  257.  
  258.  
  259.     private void paintViaBackingStore(Graphics g) {
  260.         Graphics bsg = getBackingStoreGraphics(g);
  261.         super.paint(bsg);
  262.         g.drawImage(backingStoreImage, 0, 0, this);
  263.     }
  264.  
  265.     /**
  266.      * The JViewport overrides the default implementation of
  267.      * this method (in JComponent) to return false. This ensures
  268.      * that the drawing machinery will call the Viewport's paint()
  269.      * implementation rather than messaging the JViewport's
  270.      * children directly.
  271.      *
  272.      * @return false
  273.      */
  274.     public boolean isOptimizedDrawingEnabled() {
  275.         return false;
  276.     }
  277.  
  278.     /**
  279.      * Depending on whether the backingStore is enabled,
  280.      * either paint the image through the backing store or paint
  281.      * just the recently exposed part, using the backing store
  282.      * to "blit" the remainder.
  283.      * <blockquote>
  284.      * The term "blit" is the pronounced version of the PDP-10
  285.      * BLT (BLock Transfer) instruction, which copied a bunch of
  286.      * bits. (In case you were curious.)
  287.      * </blockquote>
  288.      *
  289.      * @param g the Graphics context within which to paint
  290.      */
  291.     public void paint(Graphics g)
  292.     {
  293.         int width = getWidth();
  294.         int height = getHeight();
  295.  
  296.         if ((width == 0) || (height == 0)) {
  297.             return;
  298.         }
  299.  
  300.         if (!backingStore) {
  301.             super.paint(g);
  302.             lastPaintPosition = getViewLocation();
  303.             return;
  304.         }
  305.  
  306.         if (backingStoreImage == null) {
  307.             // Backing store is enabled but this is the first call to paint.
  308.             // Create the backing store, paint it and then copy to g.
  309.             backingStoreImage = createImage(width, height);
  310.             paintViaBackingStore(g);
  311.         }
  312.         else {
  313.             if (!scrollUnderway || lastPaintPosition.equals(getViewLocation())) {
  314.                 // No scrolling happened: repaint required area via backing store.
  315.                 paintViaBackingStore(g);
  316.             } else {
  317.                 // The image was scrolled. Manipulate the backing store and flush it to g.
  318.                 Point blitFrom = new Point();
  319.                 Point blitTo = new Point();
  320.                 Dimension blitSize = new Dimension();
  321.                 Rectangle blitPaint = new Rectangle();
  322.  
  323.                 Point newLocation = getViewLocation();
  324.                 int dx = newLocation.x - lastPaintPosition.x;
  325.                 int dy = newLocation.y - lastPaintPosition.y;
  326.                 boolean canBlit = computeBlit(dx, dy, blitFrom, blitTo, blitSize, blitPaint);
  327.                 if (!canBlit) {
  328.                     // The image was either moved diagonally or
  329.                     // moved by more than the image size: paint normally.
  330.                     paintViaBackingStore(g);
  331.                 } else {
  332.                     int bdx = blitTo.x - blitFrom.x;
  333.                     int bdy = blitTo.y - blitFrom.y;
  334.  
  335.                     // Move the relevant part of the backing store.
  336.                     Graphics bsg = getBackingStoreGraphics(g);
  337.                     bsg.copyArea(blitFrom.x, blitFrom.y, blitSize.width, blitSize.height, bdx, bdy);
  338.  
  339.                     // Paint the rest of the view; the part that has just been exposed.
  340.                     Rectangle r = getView().getBounds().intersection(blitPaint);
  341.                     bsg.setClip(r);
  342.                     super.paint(bsg);
  343.  
  344.                     // Copy whole of the backing store to g.
  345.                     g.drawImage(backingStoreImage, 0, 0, this);
  346.                 }
  347.             }
  348.         }
  349.         lastPaintPosition = getViewLocation();
  350.         scrollUnderway = false;
  351.     }
  352.  
  353.  
  354.     /** 
  355.      * Sets the bounds of this viewport. 
  356.      *      
  357.      * @param x an int giving distance from left in pixels
  358.      * @param y an int giving distance from top in pixels
  359.      * @param w an int giving width in pixels
  360.      * @param h an int giving height in pixels
  361.      *
  362.      * @see JComponent#setBounds(int, int, int, int)
  363.      */
  364.     public void setBounds(int x, int y, int w, int h) {
  365.         Dimension size = getSize();
  366.         if ((size.width != w) || (size.height != h)) {
  367.             backingStoreImage = null;
  368.         }
  369.         super.setBounds(x, y, w, h);
  370.     }
  371.  
  372.  
  373.     /**
  374.      * Returns true if this viewport is maintaining an offscreen
  375.      * image of its contents.
  376.      */
  377.     public boolean isBackingStoreEnabled() {
  378.         return backingStore;
  379.     }
  380.  
  381.  
  382.     /**
  383.      * If true if this viewport will maintain an offscreen
  384.      * image of its contents.  The image is used to reduce the cost
  385.      * of small one dimensional changes to the viewPosition.
  386.      * Rather than repainting the entire viewport we use
  387.      * Graphics.copyArea() to effect some of the scroll.
  388.      */
  389.     public void setBackingStoreEnabled(boolean x) {
  390.         backingStore = x;
  391.     }
  392.  
  393.  
  394.     /**
  395.      * Returns the Viewport's one child or null.
  396.      *
  397.      * @see #setView
  398.      */
  399.     public Component getView() {
  400.         return (getComponentCount() > 0) ? getComponent(0) : null;
  401.     }
  402.  
  403.     /**
  404.      * Sets the Viewport's one lightweight child (<code>view</code>),
  405.      * which can be null.
  406.      *
  407.      * @see #getView
  408.      */
  409.     public void setView(Component view) {
  410.  
  411.         /* Remove the viewport's existing children, if any.
  412.          * Note that removeAll() isn't used here because it
  413.          * doesn't call remove() (which JViewport overrides).
  414.          */
  415.         int n = getComponentCount();
  416.         for(int i = n - 1; i >= 0; i--) {
  417.             remove(i);
  418.         }
  419.  
  420.         isViewSizeSet = false;
  421.  
  422.         if (view != null) {
  423.             super.addImpl(view, null, -1);
  424.             viewListener = createViewListener();
  425.             view.addComponentListener(viewListener);
  426.         }
  427.     }
  428.  
  429.  
  430.     /**
  431.      * If the view's size hasn't been explicitly set, return the
  432.      * preferred size, otherwise return the view's current size.
  433.      * If there is no view, return 0,0.
  434.      *
  435.      * @return a Dimension object specifying the size of the view
  436.      */
  437.     public Dimension getViewSize() {
  438.         Component view = getView();
  439.  
  440.         if (view == null) {
  441.             return new Dimension(0,0);
  442.         }
  443.         else if (isViewSizeSet) {
  444.             return view.getSize();
  445.         }
  446.         else {
  447.             return view.getPreferredSize();
  448.         }
  449.     }
  450.  
  451.  
  452.     /**
  453.      * Sets the view coordinates that appear in the upper left
  454.      * hand corner of the viewport, and the size of the view.
  455.      *
  456.      * @param newSize a Dimension object specifying the size and
  457.      *        location of the new view coordinates, or null if there
  458.      *        is no view
  459.      */
  460.     public void setViewSize(Dimension newSize) {
  461.         Component view = getView();
  462.         if (view != null) {
  463.             Dimension oldSize = view.getSize();
  464.             if (!newSize.equals(oldSize)) {
  465.                 view.setSize(newSize);
  466.                 isViewSizeSet = true;
  467.                 fireStateChanged();
  468.             }
  469.         }
  470.     }
  471.  
  472.     /**
  473.      * Returns the view coordinates that appear in the upper left
  474.      * hand corner of the viewport, 0,0 if there's no view.
  475.      *
  476.      * @return a Point object giving the upper left coordinates
  477.      */
  478.     public Point getViewPosition() {
  479.         Component view = getView();
  480.         if (view != null) {
  481.             Point p = view.getLocation();
  482.             p.x = -p.x;
  483.             p.y = -p.y;
  484.             return p;
  485.         }
  486.         else {
  487.             return new Point(0,0);
  488.         }
  489.     }
  490.  
  491.     private Point getViewLocation() {
  492.         Component view = getView();
  493.         if (view != null) {
  494.             return view.getLocation();
  495.         }
  496.         else {
  497.             return new Point(0,0);
  498.         }
  499.     }
  500.  
  501.     /**
  502.      * Sets the view coordinates that appear in the upper left
  503.      * hand corner of the viewport, null if there's no view.
  504.      *
  505.      * @param p  a Point object giving the upper left coordinates
  506.      */
  507.     public void setViewPosition(Point p) {
  508.         Component view = getView();
  509.         if (view != null) {
  510.             // The view scrolls in the opposite direction to mouse movement.
  511.             p = new Point(-p.x, -p.y);
  512.             if (!p.equals(view.getLocation())) {
  513.                 scrollUnderway = true;
  514.                 view.setLocation(p); // This calls setBounds(), and then repaint().
  515.  
  516.                 // Mustn't do this if the view hasn't moved otherwise we loop indefinitely. (?)
  517.                 fireStateChanged();
  518.             }
  519.         }
  520.     }
  521.  
  522.  
  523.     /**
  524.      * Return a rectangle whose origin is getViewPosition and size is
  525.      * getExtentSize(). This is the visible part of the view, in view
  526.      * coordinates.
  527.      *
  528.      * @return a Rectangle giving the visible part of the view using view coordinates.
  529.      */
  530.     public Rectangle getViewRect() {
  531.         return new Rectangle(getViewPosition(), getExtentSize());
  532.     }
  533.  
  534.  
  535.     /**
  536.      * Computes the parameters for a blit where the backing store image
  537.      * currently contains oldLoc in the upper left hand corner
  538.      * and we're scrolling to newLoc.  The parameters are modified
  539.      * to return the values required for the blit.
  540.      */
  541.     protected boolean computeBlit(
  542.         int dx,
  543.         int dy,
  544.         Point blitFrom,
  545.         Point blitTo,
  546.         Dimension blitSize,
  547.         Rectangle blitPaint)
  548.     {
  549.         int dxAbs = Math.abs(dx);
  550.         int dyAbs = Math.abs(dy);
  551.         Dimension extentSize = getExtentSize();
  552.  
  553.         if ((dx == 0) && (dy != 0) && (dyAbs < extentSize.height)) {
  554.             if (dy < 0) {
  555.                 blitFrom.y = -dy;
  556.                 blitTo.y = 0;
  557.                 blitPaint.y = extentSize.height + dy;
  558.             }
  559.             else {
  560.                 blitFrom.y = 0;
  561.                 blitTo.y = dy;
  562.                 blitPaint.y = 0;
  563.             }
  564.  
  565.             blitPaint.x = blitFrom.x = blitTo.x = 0;
  566.  
  567.             blitSize.width = extentSize.width;
  568.             blitSize.height = extentSize.height - dyAbs;
  569.  
  570.             blitPaint.width = extentSize.width;
  571.             blitPaint.height = dyAbs;
  572.  
  573.             return true;
  574.         }
  575.  
  576.         else if ((dy == 0) && (dx != 0) && (dxAbs < extentSize.width)) {
  577.             if (dx < 0) {
  578.                 blitFrom.x = -dx;
  579.                 blitTo.x = 0;
  580.                 blitPaint.x = extentSize.width + dx;
  581.             }
  582.             else {
  583.                 blitFrom.x = 0;
  584.                 blitTo.x = dx;
  585.                 blitPaint.x = 0;
  586.             }
  587.  
  588.             blitPaint.y = blitFrom.y = blitTo.y = 0;
  589.  
  590.             blitSize.width = extentSize.width - dxAbs;
  591.             blitSize.height = extentSize.height;
  592.  
  593.             blitPaint.y = 0;
  594.             blitPaint.width = dxAbs;
  595.             blitPaint.height = extentSize.height;
  596.  
  597.             return true;
  598.         }
  599.  
  600.         else {
  601.             return false;
  602.         }
  603.     }
  604.  
  605.  
  606.     /**
  607.      * Returns the size of the visible part of the view in view coordinates.
  608.      *
  609.      * @return a Dimension object giving the size of the view
  610.      */
  611.     public Dimension getExtentSize() {
  612.         return getSize();
  613.     }
  614.  
  615.  
  616.     /**
  617.      * Convert a size in pixel coordinates to view coordinates.
  618.      * Subclasses of viewport that support "logical coordinates"
  619.      * will override this method.
  620.      *
  621.      * @param size  a Dimension object using pixel coordinates
  622.      * @return a Dimension object converted to view coordinates
  623.      */
  624.     public Dimension toViewCoordinates(Dimension size) {
  625.         return new Dimension(size);
  626.     }
  627.  
  628.     /**
  629.      * Convert a point in pixel coordinates to view coordinates.
  630.      * Subclasses of viewport that support "logical coordinates"
  631.      * will override this method.
  632.      *
  633.      * @param p  a Point object using pixel coordinates
  634.      * @return a Point object converted to view coordinates
  635.      */
  636.     public Point toViewCoordinates(Point p) {
  637.         return new Point(p);
  638.     }
  639.  
  640.  
  641.     /**
  642.      * Set the size of the visible part of the view using view coordinates.
  643.      *
  644.      * @param newExtent  a Dimension object specifying the size of the view
  645.      */
  646.     public void setExtentSize(Dimension newExtent) {
  647.         Dimension oldExtent = getExtentSize();
  648.         if (!newExtent.equals(oldExtent)) {
  649.             setSize(newExtent);
  650.             fireStateChanged();
  651.         }
  652.     }
  653.  
  654.     /**
  655.      * A listener for the view.
  656.      * <p>
  657.      * Warning: serialized objects of this class will not be compatible with
  658.      * future swing releases.  The current serialization support is appropriate
  659.      * for short term storage or RMI between Swing1.0 applications.  It will
  660.      * not be possible to load serialized Swing1.0 objects with future releases
  661.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  662.      * baseline for the serialized form of Swing objects.
  663.      */
  664.     protected class ViewListener extends ComponentAdapter implements Serializable
  665.     {
  666.         public void componentResized(ComponentEvent e) {
  667.             fireStateChanged();
  668.         }
  669.     }
  670.  
  671.     /**
  672.      * Create a listener for the view.
  673.      * @return a ViewListener
  674.      */
  675.     protected ViewListener createViewListener() {
  676.         return new ViewListener();
  677.     }
  678.  
  679.  
  680.     /**
  681.      * Subclassers can override this to install a different
  682.      * layout manager (or null) in the constructor.  Returns
  683.      * a new JViewportLayout object.
  684.      *
  685.      * @return a LayoutManager
  686.      */
  687.     protected LayoutManager createLayoutManager() {
  688.         return new ViewportLayout();
  689.     }
  690.  
  691.  
  692.     /**
  693.      * Add a ChangeListener to the list that's notified each time the view's
  694.      * size, position, or the viewport's extent size has changed.
  695.      *
  696.      * @param l the ChangeListener to add
  697.      * @see #removeChangeListener
  698.      * @see #setViewPosition
  699.      * @see #setViewSize
  700.      * @see #setExtentSize
  701.      */
  702.     public void addChangeListener(ChangeListener l) {
  703.         listenerList.add(ChangeListener.class, l);
  704.     }
  705.  
  706.     /**
  707.      * Remove a ChangeListener from the list that's notified each
  708.      * time the views size, position, or the viewports extent size
  709.      * has changed.
  710.      *
  711.      * @param l the ChangeListener to remove
  712.      * @see #addChangeListener
  713.      */
  714.     public void removeChangeListener(ChangeListener l) {
  715.         listenerList.remove(ChangeListener.class, l);
  716.     }
  717.  
  718.  
  719.     /*
  720.      * Notify all ChangeListeners when the views
  721.      * size, position, or the viewports extent size has changed.
  722.      *
  723.      * @see #addChangeListener
  724.      * @see #removeChangeListener
  725.      * @see EventListenerList
  726.      */
  727.     protected void fireStateChanged()
  728.     {
  729.         Object[] listeners = listenerList.getListenerList();
  730.         for (int i = listeners.length - 2; i >= 0; i -= 2) {
  731.             if (listeners[i] == ChangeListener.class) {
  732.                 if (changeEvent == null) {
  733.                     changeEvent = new ChangeEvent(this);
  734.                 }
  735.                 ((ChangeListener)listeners[i + 1]).stateChanged(changeEvent);
  736.             }
  737.         }
  738.     }
  739.  
  740.     /** 
  741.      * We always repaint in our parent coordinate system to make sure
  742.      * only one paint is performed by the RepaintManager.
  743.      *
  744.      * @param     tm   maximum time in milliseconds before update
  745.      * @param     x    the <i>x</i> coordinate (pixels over from left)
  746.      * @param     y    the <i>y</i> coordinate (pixels down from top)
  747.      * @param     width    the width
  748.      * @param     height   the height
  749.      * @see       java.awt.Component#update(java.awt.Graphics)
  750.      */
  751.     public void repaint(long tm, int x, int y, int w, int h) {
  752.         Container parent = getParent();
  753.         if(parent != null)
  754.             parent.repaint(tm,x+getX(),y+getY(),w,h);
  755.         else
  756.             super.repaint(tm,x,y,w,h);
  757.     }
  758.  
  759.  
  760. /////////////////
  761. // Accessibility support
  762. ////////////////
  763.  
  764.     /**
  765.      * Get the AccessibleContext associated with this JComponent
  766.      *
  767.      * @return the AccessibleContext of this JComponent
  768.      */
  769.     public AccessibleContext getAccessibleContext() {
  770.         if (accessibleContext == null) {
  771.             accessibleContext = new AccessibleJViewport();
  772.         }
  773.         return accessibleContext;
  774.     }
  775.  
  776.     /**
  777.      * The class used to obtain the accessible role for this object.
  778.      * <p>
  779.      * Warning: serialized objects of this class will not be compatible with
  780.      * future swing releases.  The current serialization support is appropriate
  781.      * for short term storage or RMI between Swing1.0 applications.  It will
  782.      * not be possible to load serialized Swing1.0 objects with future releases
  783.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  784.      * baseline for the serialized form of Swing objects.
  785.      */
  786.     protected class AccessibleJViewport extends AccessibleJComponent {
  787.         /**
  788.          * Get the role of this object.
  789.          *
  790.          * @return an instance of AccessibleRole describing the role of
  791.          * the object
  792.          */
  793.         public AccessibleRole getAccessibleRole() {
  794.             return AccessibleRole.VIEWPORT;
  795.         }
  796.     } // inner class AccessibleJViewport
  797. }
  798.